package com.clg.protobuf;

import com.clg.protobuf.filter.GzipFilter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter;
import org.springframework.web.filter.CharacterEncodingFilter;

/**
 * Created by chenlong on 2017/5/26.
 * extends SpringBootServletInitializer 支持部署到自己的tomcat
 */
@SpringBootApplication
public class ProtobufApplication extends SpringBootServletInitializer {

	/**
	 * 支持 Protobuf 数据传输
	 * @return
	 */
	@Bean
	public ProtobufHttpMessageConverter protobufHttpMessageConverter() {
		return new ProtobufHttpMessageConverter();
	}

	/**
	 * 支持部署到自己的tomcat
	 * @param application
	 * @return
	 */
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(ProtobufApplication.class);
	}

	/**
	 * gzip filter
	 * @return
	 */
	@Bean
	public FilterRegistrationBean gzipFilterRegistration() {
		GzipFilter gzipFilter = new GzipFilter();
		return new FilterRegistrationBean(gzipFilter);
	}

	/**
	 * 加载字符过滤器
	 * @return
	 */
	@Bean
	public FilterRegistrationBean characterFilterRegistration() {
		CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
		characterEncodingFilter.setForceEncoding(true);
		characterEncodingFilter.setEncoding("utf-8");
		return new FilterRegistrationBean(characterEncodingFilter);
	}

	public static void main(String[] args) {
		SpringApplication.run(ProtobufApplication.class, args);
	}
}
